#coding:utf-8
import os
from flask import Flask,url_for,render_template,request,url_for,redirect,send_from_directory
from addMark import addWaterMarking
from testMark import testWaterMarking


UPLOAD_FOLDER='home'
ALLOWED_EXTENSIONS=set(['png','jpg','jpeg','gif'])

app=Flask(__name__)
global strMark
strMark = ' '
app.config['UPLOAD_FOLDER']=UPLOAD_FOLDER

def allowed_file(filename):
    return '.' in filename and filename.rsplit('.',1)[1] in ALLOWED_EXTENSIONS

@app.route('/',methods=['GET','POST'])
def upload_file():
    if request.method=='POST':
        file1=request.files['file']
        file2=request.files['file_mask']
        if file1 and allowed_file(file1.filename) and file2 and allowed_file(file2.filename):
            addWaterMarking(file1,file2)
            return redirect(url_for('upload_file'))
    return '''
    <!DOCTYPE html>
    <html>
    <head>
        <title>Upload New File</title>
        <style>
            body {
                font-family: Arial, sans-serif;
                margin: 0;
                padding: 0;
                background-color: #f0f0f0;
            }
            .container {
                width: 80%;
                margin: 0 auto;
                padding: 20px;
                box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
                background-color: #fff;
                border-radius: 5px;
            }
            h1, h2 {
                color: #333;
            }
            input[type="file"] {
                margin: 10px 0;
                padding: 10px;
                border: 1px solid #ddd;
                border-radius: 5px;
            }
            input[type="submit"] {
                background-color: #4CAF50;
                color: white;
                padding: 10px 20px;
                border: none;
                border-radius: 4px;
                cursor: pointer;
                transition: background-color 0.3s ease;
            }
            input[type="submit"]:hover {
                background-color: #45a049;
            }
            a {
                color: #4CAF50;
                text-decoration: none;
            }
            a:hover {
                text-decoration: underline;
            }
        </style>
    </head>
    <body>
        <div class="container">
            <h1>Add Digital Watermarking</h1>
            <h2>Choose picture</h2>
            <form action="/" method="POST" enctype="multipart/form-data">
                <input type="file" name="file" />
                <h2>Choose Watermarking</h2>
                <input type="file" name="file_mask" />
                <p>
                    <input type="submit" value="Handle" />
                </p>
            </form>
            <a href="test">Test Watermarking</a>
            <p>
                <script type="text/javascript">  
                    document.write("<img src='../static/output.png?v="+new Date().getTime()+"'>");   
                </script>  
            </p>
        </div>
    </body>
    </html>
    '''


@app.route('/test',methods=['GET','POST'])
def test_file():
    global strMark
    if request.method=='POST':
        file1=request.files['file']
        file2=request.files['file_mask']
        if file1 and allowed_file(file1.filename) and file2 and allowed_file(file2.filename):
            boolMark = testWaterMarking(file1,file2)
            if boolMark == False:
                strMark = 'False'
            else:
                strMark = 'True'
        else:
                # 如果没有file2，执行提取图片的操作
                # 假设你有一个名为extract_image的函数
                testWaterMarking(file1)
        return redirect(url_for('test_file'))
    return '''
<!DOCTYPE html>
<html>
<head>
    <title>Upload New File</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #f0f0f0;
        }
        .container {
            width: 80%;
            margin: 0 auto;
            padding: 20px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            background-color: #fff;
            border-radius: 5px;
        }
        h1, h2 {
            color: #333;
        }
        input[type="file"] {
            margin: 10px 0;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 5px;
        }
        input[type="submit"] {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s ease;
        }
        input[type="submit"]:hover {
            background-color: #45a049;
        }
        a {
            color: #4CAF50;
            text-decoration: none;
        }
        a:hover {
            text-decoration: underline;
        }
    </style>
</head>
<body>
    <div class="container">
        <h1>Test Digital Watermarking</h1>
        <h2>Choose picture</h2>
        <form action="/test" method="POST" enctype="multipart/form-data">
            <input type="file" name="file" />
            <h2>Choose Watermarking</h2>
            <input type="file" name="file_mask" />
            <p>
                <input type="submit" value="Handle" />
            </p>
        </form>
        <a href="/">Add Watermarking</a>
        <p>
            ''' + strMark+  '''
        </p>
        <p>
            <script type="text/javascript">  
                document.write("<img src='../static/extracted_watermark.png?v="+new Date().getTime()+"' alt='Extracted Watermark'>");   
            </script>  
        </p>
    </div>
</body>
</html>
    '''

@app.route('/uploads/<filename>')
def uploaded_file(filename):
    return send_from_directory(app.config['UPLOAD_FOLDER'],filename)


if __name__ == '__main__':
    strMark = ''
    app.run()